package cn.zjxiliu.lib_login.server;

import org.xutils.common.Callback;

import cn.zjxiliu.lib_core.http.BaseRequest;
import cn.zjxiliu.lib_core.http.HttpCore;
import cn.zjxiliu.lib_core.http.RequestCallback;
import cn.zjxiliu.lib_login.constants.UrlConstants;
import cn.zjxiliu.lib_login.server.callback.CheckPhoneHasRegisterCallback;
import cn.zjxiliu.lib_login.server.callback.ForgetPwdCallback;
import cn.zjxiliu.lib_login.server.callback.GetVerificationCodeCallback;
import cn.zjxiliu.lib_login.server.callback.LoginCallback;
import cn.zjxiliu.lib_login.server.request.CheckPhoneHasRegisterRequest;
import cn.zjxiliu.lib_login.server.request.ForgetPwdRequest;
import cn.zjxiliu.lib_login.server.request.LoginRequest;
import cn.zjxiliu.lib_login.server.request.RegisterRequest;
import cn.zjxiliu.lib_login.server.request.VerificationCodeRequest;
import cn.zjxiliu.lib_login.server.response.CheckPhoneHasRegisterResponse;
import cn.zjxiliu.lib_login.server.response.ForgetPwdResponse;
import cn.zjxiliu.lib_login.server.response.GetVerificationCodeResponse;
import cn.zjxiliu.lib_login.server.response.LoginResponse;

/**
 * Created by jessie on 2018/11/21.
 */

public class HttpManager implements UrlConstants {

    private static HttpManager httpManager = new HttpManager();


    public static HttpManager getInstance(){
        return httpManager;
    }


    public HttpManager init(String baseRequestUrl){
        HttpCore.getInstance().init(baseRequestUrl);
        return this;
    }


    public Callback.Cancelable startLogin(LoginRequest loginRequest, final LoginCallback callback){
        return postRequest(loginRequest, LOGIN, new RequestCallback<LoginResponse>() {
            @Override
            protected void onTokenUnusefullCallback() {

            }

            @Override
            protected void onFinishedCallback() {

            }

            @Override
            public void onSuccessCallback(LoginResponse loginResponse) {
                if(callback != null){
                    callback.onLoginSuccess(loginResponse);
                }
            }

            @Override
            public void onFailedCallback(String errorMsg) {
                if(callback != null){
                    callback.onOperateFailed(errorMsg);
                }
            }

            @Override
            public void onCancelledCallback(String message) {
                if(callback != null){
                    callback.onOperateFailed(message);
                }
            }
        });
    }



    public Callback.Cancelable startForgetPwd(ForgetPwdRequest request, final ForgetPwdCallback callback){
        return postRequest(request, FORGET_PWD, new RequestCallback<ForgetPwdResponse>() {
            @Override
            protected void onTokenUnusefullCallback() {

            }

            @Override
            protected void onFinishedCallback() {

            }

            @Override
            public void onSuccessCallback(ForgetPwdResponse forgetPwdResponse) {
                if(callback != null){
                    callback.onForgetPwdSuccess();
                }
            }

            @Override
            public void onFailedCallback(String errorMsg) {
                if(callback != null){
                    callback.onOperateFailed(errorMsg);
                }
            }

            @Override
            public void onCancelledCallback(String message) {
                if(callback != null){
                    callback.onOperateFailed(message);
                }
            }
        });
    }



    public Callback.Cancelable startRegister(RegisterRequest registerRequest, final LoginCallback callback){
        return postRequest(registerRequest, REGISTER, new RequestCallback<LoginResponse>() {
            @Override
            protected void onTokenUnusefullCallback() {

            }

            @Override
            protected void onFinishedCallback() {

            }

            @Override
            public void onSuccessCallback(LoginResponse loginResponse) {
                if(callback != null){
                    callback.onLoginSuccess(loginResponse);
                }
            }

            @Override
            public void onFailedCallback(String errorMsg) {
                if(callback != null){
                    callback.onOperateFailed(errorMsg);
                }
            }

            @Override
            public void onCancelledCallback(String message) {
                if(callback != null){
                    callback.onOperateFailed(message);
                }
            }
        });
    }


    public Callback.Cancelable checkPhoneHasRegister(CheckPhoneHasRegisterRequest request, final CheckPhoneHasRegisterCallback callback) {
        return postRequest(request, CHECK_REGISTER, new RequestCallback<CheckPhoneHasRegisterResponse>() {
            @Override
            protected void onTokenUnusefullCallback() {

            }

            @Override
            protected void onFinishedCallback() {

            }

            @Override
            public void onSuccessCallback(CheckPhoneHasRegisterResponse checkPhoneHasRegisterResponse) {
                if(callback != null){
                    callback.onPhoneHasRegisterSuccess(checkPhoneHasRegisterResponse.isRegister());
                }
            }


            @Override
            public void onFailedCallback(String errorMsg) {
                if(callback != null){
                    callback.onOperateFailed(errorMsg);
                }
            }

            @Override
            public void onCancelledCallback(String message) {
                if(callback != null){
                    callback.onOperateFailed(message);
                }
            }
        });


    }


    public Callback.Cancelable getVerificationCode(VerificationCodeRequest request, final GetVerificationCodeCallback callback){
        return postRequest(request, VERIFIVATION, new RequestCallback<GetVerificationCodeResponse>() {
            @Override
            protected void onTokenUnusefullCallback() {

            }

            @Override
            protected void onFinishedCallback() {

            }

            @Override
            public void onSuccessCallback(GetVerificationCodeResponse getVerificationCodeResponse) {
                if(callback != null){
                    callback.onGetVerificationCodeSuccess();
                }
            }

            @Override
            public void onFailedCallback(String errorMsg) {
                if(callback != null){
                    callback.onOperateFailed(errorMsg);
                }
            }

            @Override
            public void onCancelledCallback(String message) {
                if(callback != null){
                    callback.onOperateFailed(message);
                }
            }
        });
    }

    private <T extends BaseRequest, S> Callback.Cancelable postRequest(T requestBean, String requestPath, RequestCallback<S> callback){
        return HttpCore.getInstance().postSend(requestBean, requestPath, callback);
    }



}
